﻿using HalconDotNet;
using vciplib0;
using Newtonsoft.Json;
using PCBase.UIControls;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Linq;
using System.Text;
using GraphicROI;


namespace vciplib0
{
    public class PRM_RegionDetect
    {
        [Category("检测区域"), DisplayName("检测区域"), Description("检测区域，并集，差集"), Browsable(false)]
        public List<PRM_ROI> ROIs { get; set; } = new List<PRM_ROI>();

        /// <summary>
        ///  GrahicROI 绘制的ROI 导出 Region
        /// </summary>
        [NonSerialized]
        public HRegion DetectRegion = null;

        /// <summary>
        /// 批量Bypass Region
        /// </summary>
        [NonSerialized]
        public HRegion BatchBypassRegion = null;

        [NonSerialized]
        public HRegion AOIDetectRegion = null;


        [Browsable(false)]
        public string TagId = "";//时间标记ID

        /// <summary>
        /// GrahicROI 绘制的ROI 导出 Region
        /// </summary>
        /// <returns></returns>
        public HRegion GenDetectRegion()
        {
            double row, col;
            HRegion tmp = new HRegion();
            tmp.GenEmptyRegion();

            HRegion tmpAdd, tmpDiff;

            tmpAdd = new HRegion();
            tmpDiff = new HRegion();  // rojor region 实例化之后 调用 GenEmptyRegion()
            tmpAdd.GenEmptyRegion();
            tmpDiff.GenEmptyRegion();
            foreach (var item in ROIs)
            {
                ROI roi = item.GetViewROI();
                roi.setOperatorFlag(item.ROIMode); //设置ROI的模式
                switch (roi.getOperatorFlag())
                {
                    case ROI.POSITIVE_FLAG:
                        tmp = roi.getRegion();
                        tmpAdd = tmp.Union2(tmpAdd);
                        break;
                    case ROI.NEGATIVE_FLAG:
                        tmp = roi.getRegion();
                        tmpDiff = tmp.Union2(tmpDiff);
                        break;
                    default:
                        break;
                }
            }

            if (tmpAdd.AreaCenter(out row, out col) > 0)
            {
                tmp = tmpAdd.Difference(tmpDiff);
            }

            //DetectRegion = tmp;
            return tmp;
        }

        public override string ToString()
        {
            return string.Format("{0}-{1}- RoiNo:{2}", DetectClassificationName, DetectRemark, ROIs.Count);
        }

        [Category("1.检测项目"), DisplayName("a.检测类名"), Description("1.SurfaceDetect宏观比对缺陷检, 2.Chipping划线崩边检, 3.Measure_street_width量测切割道的宽度"), Browsable(true)]
        [EditorAttribute(typeof(CommonInputEditor), typeof(System.Drawing.Design.UITypeEditor)), TypeConverter(typeof(DefectTypeDefine))]
        public string DetectClassificationName { get; set; } = "1.SurfaceDefect";  // "Chipping"

        [Category("1.检测项目"), DisplayName("b.备注信息"), Description("检测项目备注信息"), Browsable(true)]
        public string DetectRemark { get; set; } = "";

        [CategoryAttribute("1.检测项目"), DisplayName("c.是否使用GoldenImage"), Description("是否与标准模板做对比"), Browsable(false)]
        public bool isCompareGoldenModel { get; set; } = true;

        [CategoryAttribute("1.检测项目"), DisplayName("d.Bright区域检测"), Description("灰阶值 大于 设定阈值，判为 Bright Defect"), Browsable(false)]
        public bool Bright_Enabe { get; set; } = true;

        [CategoryAttribute("1.检测项目"), DisplayName("e.Dark区域检测"), Description("灰阶值 小于 设定阈值，判为 Dark Defect"), Browsable(false)]
        public bool Dark_Enabe { get; set; } = true;

        [CategoryAttribute("1.检测项目"), DisplayName("f.Bump检测使能"), Description("Bump检测项目是否启用，True:使用Bump参数检测 False：Bump区使用Bright和Dark参数检测。"), Browsable(false)]
        public bool Bump_Enabe { get; set; } = true;

        [CategoryAttribute("1.检测项目"), DisplayName("g.使用Bright,Dark相同参数"), Description("True:启用, False:禁用"), Browsable(false)]
        public bool SamePrmEnable { get; set; } = true;

        private double _bright_MinDefectArea = 25;
        [CategoryAttribute("2.检测参数 Bright"), DisplayName("a.最小Defect面积 [Bright]"), Description("Bright 最小区域面积，单位um. 默认值: 25"), Browsable(true)]
        public Double Bright_MinDefectArea
        {
            get
            {
                return _bright_MinDefectArea;
            }
            set
            {
                if (value < 1) value = 1;
                if (value > 9999999999) value = 9999999999;
                _bright_MinDefectArea = value;
            }
        }

        private double _bright_MinDefectWidth = 5;
        [CategoryAttribute("2.检测参数 Bright"), DisplayName("b.最小Defect宽度 [Bright]"), Description("Bright 最小区域宽度，单位um. 默认值:5"), Browsable(true)]
        public Double Bright_MinDefectWidth
        {
            get
            {
                return _bright_MinDefectWidth;
            }
            set
            {
                if (value < 1) value = 1;
                if (value > 999999) value = 999999;
                _bright_MinDefectWidth = value;
            }
        }

        private double _bright_MinDefectLength = 5;
        [CategoryAttribute("2.检测参数 Bright"), DisplayName("c.最小Defect长度 [Bright]"), Description("Bright 最小区域长度，单位um. 默认值:5"), Browsable(true)]
        public Double Bright_MinDefectLength
        {
            get
            {
                return _bright_MinDefectLength;
            }
            set
            {
                if (value < 1) value = 1;
                if (value > 999999) value = 999999;
                _bright_MinDefectLength = value;
            }
        }

        private double _bright_MinContrastDalta = 25;
        [CategoryAttribute("2.检测参数 Bright"), DisplayName("d.Defect灰阶差阈值 [Bright]"), Description("Bright Defect灰阶差阈值 设置范围[20 - 255] ,默认值 25"), Browsable(true)]
        public Double Bright_MinContrastDalta
        {
            get
            {
                return _bright_MinContrastDalta;
            }
            set
            {
                if (value < 10) value = 10;
                if (value > 255) value = 255;
                _bright_MinContrastDalta = value;
            }
        }

        [CategoryAttribute("2.检测参数 Bright"), DisplayName("e.边缘灰阶差比例系数 [Bright]"), Description("Bright Defect边缘灰阶差系数。设置范围[1.0 - 2.0]"), Browsable(false)]
        public Double Bright_EdgeFactor { get; set; } = 1.2;

        [CategoryAttribute("2.检测参数 Bright"), DisplayName("f.Defect Color [Bright]"), Description("Bright Defect 标记颜色"), Browsable(false)]
        public Color Bright_DefectColor { get; set; } = Color.Red;

        private double _dark_MinDefectArea = 25;
        [CategoryAttribute("3.检测参数 Dark"), DisplayName("a.最小Defect面积 [Dark]"), Description("Dark 最小区域面积，单位um. 默认值: 25"), Browsable(true), ReadOnly(true)]
        public Double Dark_MinDefectArea
        {
            get
            {
                return _dark_MinDefectArea;
            }
            set
            {
                if (value < 1) value = 1;
                if (value > 9999999999) value = 9999999999;
                _dark_MinDefectArea = value;
            }
        }

        private double _dark_MinDefectWidth = 5;
        [CategoryAttribute("3.检测参数 Dark"), DisplayName("b.最小Defect宽度 [Dark]"), Description("Dark 最小区域宽度，单位um. 默认值:5"), Browsable(true), ReadOnly(true)]
        public Double Dark_MinDefectWidth
        {
            get
            {
                return _dark_MinDefectWidth;
            }
            set
            {
                if (value < 1) value = 1;
                if (value > 999999) value = 999999;
                _dark_MinDefectWidth = value;
            }
        }

        private double _dark_MinDefectLength = 5;
        [CategoryAttribute("3.检测参数 Dark"), DisplayName("c.最小Defect长度 [Dark]"), Description("Dark 最小区域长度，单位um. 默认值:5"), Browsable(true), ReadOnly(true)]
        public Double Dark_MinDefectLength
        {
            get
            {
                return _dark_MinDefectLength;
            }
            set
            {
                if (value < 1) value = 1;
                if (value > 999999) value = 999999;
                _dark_MinDefectLength = value;
            }
        }

        private double _dark_MinContrastDalta = 25;

        private static  int measureStreetWidthRowSpliteNum = 2;
        private static int measureStreetWidthColSpliteNum = 2;

        [CategoryAttribute("3.检测参数 Dark"), DisplayName("d.Defect灰阶差阈值 [Dark]"), Description("Dark Defect灰阶差阈值 设置范围[20 - 255] ,默认值 25"), Browsable(true), ReadOnly(true)]
        public Double Dark_MinContrastDalta
        {
            get
            {
                return _dark_MinContrastDalta;
            }
            set
            {
                if (value < 10) value = 10;
                if (value > 255) value = 255;
                _dark_MinContrastDalta = value;
            }
        }

        [CategoryAttribute("3.检测参数 Dark"), DisplayName("e.边缘灰阶差比例系数 [Dark]"), Description("Dark Defect边缘灰阶差系数。设置范围[1.0 - 2.0]"), Browsable(false)]
        public Double Dark_EdgeFactor { get; set; } = 1.2;

        [CategoryAttribute("3.检测参数 Dark"), DisplayName("f.Defect Color [Dark]"), Description("Dark Defect 标记颜色"), Browsable(false)]
        public Color Dark_DefectColor { get; set; } = Color.Red;



        [CategoryAttribute("4.崩边参数"), DisplayName("a.崩边灰阶阈值"), Description("崩边灰阶阈值.默认值: 25.数值越大,检测越严格"), Browsable(false)]
        public Double Chipping_MinThres { get; set; } = 25;

        [Category("4.崩边参数"), DisplayName("b.崩边检测屏蔽边"), Description("选择需要屏蔽的边, 默认不屏蔽"), Browsable(false)]
        [EditorAttribute(typeof(CommonInputEditor), typeof(System.Drawing.Design.UITypeEditor)), TypeConverter(typeof(ChippingShieldType))]
        public string Chipping_Shield { get; set; } = "不屏蔽";

        //[Category("4.切割道宽度量测"), DisplayName("a.切割道上限宽度(单位:um)"), Description(""), Browsable(false)]
        //public double MeasureStreetWidthLimitWidth
        //{ 
        //    get => measureStreetWidthLimitWidth; 
        //    set
        //    {
        //        if(value<0)
        //        {
        //            measureStreetWidthLimitWidth = 1;
        //        }
        //        else
        //        {
        //            measureStreetWidthLimitWidth = value;
        //        }
        //    }
        //}

        [Obsolete]
        [Category("4.切割道宽度量测"), DisplayName("b.切割道宽度量测屏蔽边"), Description("选择需要屏蔽的边, 默认不屏蔽"), Browsable(false)]
        [EditorAttribute(typeof(CommonInputEditor), typeof(System.Drawing.Design.UITypeEditor)), TypeConverter(typeof(ChippingShieldType))]
        public string MeasureStreetWidthShield { get; set; } = "不屏蔽";

        [Category("4.复判拍照参数"), DisplayName("1.彩色复判拍照次数上限"), Description("每颗ChipDie Defect 彩色复判拍照数量上限"), ReadOnly(false), Browsable(true)]
        public int DefectReCheckCount { get; set; } = 5;
        [Browsable(false)]
        public List<string> MeasureStreetWidthLimitRegionNames { get; set; }


        [Category("4.切割道宽度量测"), DisplayName("a.行抽检间隔"), Description(""), Browsable(false)]
        public int MeasureStreetWidthRowSpliteNum
        {
            get => measureStreetWidthRowSpliteNum;
            set
            {
                if (value < 2)
                {
                    measureStreetWidthRowSpliteNum = 2;
                }
                else
                {
                    measureStreetWidthRowSpliteNum = value;
                }
            }
        }
        [Category("4.切割道宽度量测"), DisplayName("a.列抽检间隔"), Description(""), Browsable(false)]

        public int MeasureStreetWidthColSpliteNum
        {
            get => measureStreetWidthColSpliteNum;
            set
            {
                if (value < 2)
                {
                    measureStreetWidthColSpliteNum = 2;
                }
                else
                {
                    measureStreetWidthColSpliteNum = value;

                }
            }
        }
    }
}
